package com.java.reflect;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectClient {

	public static void main(String[] args) throws Exception {
		//Apple apple = new Apple(100,"红富士");
		//Apple o = copyObject(apple);
		//System.out.println(o);
		arrayTest2();
	}

	public static <T> T copyObject(T obj) throws Exception{
		Class<?> c = obj.getClass();
		Constructor constructor = c.getConstructor(new Class[]{});
		T nobj = (T)constructor.newInstance(new Object[]{});
		Field[] fields = c.getDeclaredFields();
		for(Field field : fields){
			String fieldName = field.getName();
			fieldName = fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
			Method getMethod = c.getMethod("get" + fieldName);
			Method setMethod = c.getMethod("set" + fieldName,field.getType());
			Object value = getMethod.invoke(obj,new Object[]{});
			setMethod.invoke(nobj,new Object[]{value});
		}
		return nobj;
	}

	public static void arrayTest(){
		Object arr = Array.newInstance(String.class,1);
		Array.set(arr,0,"0s");
		System.out.println(Array.get(arr, 0));
	}

	public static void arrayTest2(){
		int[] is = new int[]{3,3,3};
		Object arr = Array.newInstance(Integer.TYPE,is);
		//System.out.println(arr instanceof int[][][]);
		for(int i=0;i<3;i++){
			Object ai = Array.get(arr,i);
			//System.out.println(ai.getClass().getComponentType().getName());
			for(int j=0;j<3;j++){
				Object aj = Array.get(ai,j);
				for(int k=0;k<3;k++){
					Array.set(aj,k,k);
				}
			}
		}
		int[][][] r = (int[][][])arr;
	}


}
